package com.ruoyi.web.controller.system.api.cc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.Authentication;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.ReportInfo;
import com.ruoyi.system.api.domain.StErrorReport;
import com.ruoyi.system.api.service.CcService;
import com.ruoyi.system.api.service.IAnnexService;
import com.ruoyi.system.api.service.IReportInfoService;
import com.ruoyi.system.api.service.IStErrorReportService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * ErrorReport报告
 *
 * @author yzg
 * @ClassName AcceptanceReportController.java
 * @date 2020/3/9 8:41 上午
 */
@Api(tags = "ErrorReport报告")
@RestController
@RequestMapping("/api/cc/error/report/")
public class ErrorReportController extends BaseController {

    @Autowired
    private IStErrorReportService errorReportService;

    @Autowired
    private IAnnexService annexService;

    @Autowired
    private CcService ccService;
    @Autowired
    private IReportInfoService reportInfoService;
    private static final Logger log = LoggerFactory.getLogger(ErrorReportController.class);


    /**
     * 添加ErrorReport报告
     *
     * @param errorReport
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/5 1:27 下午
     */
    @Log(title = "添加ErrorReport报告", businessType = BusinessType.INSERT)
    @PostMapping("addErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "添加ErrorReport报告", httpMethod = "POST", notes = "添加ErrorReport报告")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> addErrorReport(@RequestBody StErrorReport errorReport) {
        try {
            return errorReportService.addErrorReport(errorReport);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 修改ErrorReport报告
     *
     * @param errorReport
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/5 1:28 下午
     */
    @Log(title = "修改ErrorReport报告", businessType = BusinessType.INSERT)
    @PostMapping("updateErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "修改ErrorReport报告", httpMethod = "POST", notes = "修改ErrorReport报告")
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateErrorReport(@RequestBody StErrorReport errorReport) {
        try {
            return errorReportService.updateErrorReport(errorReport);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 提交Error report
     *
     * @param id
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/7 11:26 上午
     */
    @RepeatSubmit
    @Log(title = "提交Error report", businessType = BusinessType.UPDATE)
    @GetMapping("errorReportSubmit")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "提交Error report", httpMethod = "GET", notes = "提交Error report")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "提交提交Error report主键ID", name = "id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(value = "提交的工程师ID", name = "enginnerId", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> errorReportSubmit(@RequestParam Long id, @RequestParam Long enginnerId) {
        try {
            return errorReportService.errorReportSubmit(id, enginnerId);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查看ErrorReport报告
     *
     * @param id
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/5 1:25 下午
     */
    @GetMapping("detailErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看ErrorReport报告", httpMethod = "GET", notes = "查看ErrorReport报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "ErrorReport主键ID", name = "id", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> detailErrorReport(@RequestParam(value = "id", required = true) String id) {
        try {

            return errorReportService.detailErrorReport(Long.parseLong(id));
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查看CCErrorReport报告
     *
     * @param id
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/5 1:25 下午
     */
    @GetMapping("detailCCErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看CCErrorReport报告", httpMethod = "GET", notes = "查看ErrorReport报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师主键ID", name = "EnginnerId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "serviceNo", name = "serviceNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "ErrorReport主键ID", name = "reportGuid", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> detailCCErrorReport(@RequestParam(value = "EnginnerId", required = true) String EnginnerId,
                                                   @RequestParam(value = "serviceNo", required = false) String serviceNo,
                                                   @RequestParam(value = "reportGuid", required = false) String reportGuid) {
        try {
            Map<String, Object> map = new HashMap();
            map.put("enginner_id", EnginnerId);
            map.put("service_no", serviceNo);
            map.put("report_guid", reportGuid);
            return errorReportService.detailCCErrorReport(map);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 查看ErrorReport报告图片信息
     *
     * @param serviceNo
     * @param helpId
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/3/5 1:36 下午
     */
    @GetMapping("selectErrorReportAnnexs")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看ErrorReport报告图片信息", httpMethod = "GET", notes = "查看ErrorReport报告图片信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "serviceNo", name = "serviceNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "报告辅助id", name = "helpId", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> selectErrorReportAnnexs(@RequestParam Long serviceNo, @RequestParam Long helpId) {
        try {
            return annexService.selectAnnexsByServiceNoAndHelpId(serviceNo.toString(), helpId.toString());
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 查看ErrorReport列表
     *
     * @param serviceNo
     * @param enginnerId
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sunjy
     * @date 2020年3月13日15:42:42
     */
    @GetMapping("selectErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看ErrorReport列表", httpMethod = "GET", notes = "查看ErrorReport列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "serviceNo", name = "serviceNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "计划id", name = "planId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师姓名", name = "fseName", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "创建时间 （YYYY-MM-dd）", name = "createTime", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> selectErrorReportList(@RequestParam(value = "serviceNo", required = false) String serviceNo,
                                                     @RequestParam(value = "enginnerId", required = false) Long enginnerId,
                                                     @RequestParam(value = "planId", required = false) Long planId,
                                                     @RequestParam(value = "fseName", required = false) String fseName,
                                                     @RequestParam(value = "createTime", required = false) String createTime) {
        try {
            List<ReportInfo> list = new ArrayList<>();
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setServiceNo(serviceNo);
            reportInfo.setEnginnerId(enginnerId);
            reportInfo.setPlanId(planId);
            reportInfo.setFseName(fseName);
            if (StringUtils.isNotEmpty(createTime)) {
                Date date = DateUtils.parseDate(createTime);
                if (date != null) {
                    reportInfo.setStartTime(createTime + " 00:00:00");
                    reportInfo.setEndTime(createTime + " 23:59:59");
                }
            }
            reportInfo.setRelServiceNo(2L);
            list = errorReportService.selectStErrorReportList(reportInfo);
            return new ResponseWithData<>().success(list).code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }


    /**
     * 查看ErrorCode
     *
     * @param enginnerId
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author sunjy
     * @date 2020年3月13日15:42:42
     */
    @GetMapping("getErrorCode")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "查看ErrorCode", httpMethod = "GET", notes = "查看ErrorCode")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", required = true, dataType = "Long", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> getErrorCode(@RequestParam(value = "enginnerId", required = false) Long enginnerId) {
        try {
            List paramTables = ccService.getErrorCode(enginnerId);
            return new ResponseWithData().success(paramTables).code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 删除ErrorReport报告
     *
     * @param id
     * @param enginnerId
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/4/20 2:17 下午
     */
    @Log(title = "删除ErrorReport报告", businessType = BusinessType.DELETE)
    @GetMapping("delErrorReport")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "删除ErrorReport报告", httpMethod = "GET", notes = "删除ErrorReport报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "报告id", name = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "工程师ID", name = "enginnerId", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> delErrorReport(@RequestParam("id") Long id, @RequestParam("enginnerId") Long enginnerId) {
        try {
            return errorReportService.delErrorReport(id, enginnerId);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }

    /**
     * 修改ErrorReport报告状态
     *
     * @param id
     * @param enginnerId
     * @return com.ruoyi.system.api.ResponseWithData<?>
     * @author yzg
     * @date 2020/4/20 2:17 下午
     */
    @Log(title = "修改ErrorReport报告", businessType = BusinessType.UPDATE)
    @GetMapping("updateStatus")
    @ResponseBody
    @Authentication(tokenRequired = true, signRequired = true)
    @ApiOperation(value = "修改ErrorReport报告", httpMethod = "GET", notes = "修改ErrorReport报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "请求编号", name = "reptNo", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(value = "状态", name = "status", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 500, message = "code 为 500 系统异常"),
            @ApiResponse(code = 200, message = "code 为 200 成功")
    })
    public ResponseWithData<?> updateStatus(@RequestParam("reptNo") String reptNo, @RequestParam("status") String status) {
        try {
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setReportNo(reptNo);
            reportInfo.setStatus(status);
            reportInfoService.updateStReportInfoByReportNo(reportInfo);
            return new ResponseWithData().success().msg("更新成功").code(200);
        } catch (Exception e) {
            log.error("程序异常", e.getMessage());
            return new ResponseWithData().error().msg("系统异常").code(500);
        }
    }
}
